package com.gxuwz.medical.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jdt.internal.compiler.flow.InsideSubRoutineFlowContext;

import com.gxuwz.medical.domain.area.Area;
import com.gxuwz.medical.domain.institution.Institution;

/**
 * 农合经办机构管理控制处理模块
 * @author 演示
 *
 */
public class InstitutionServlet extends BaseServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String  m=request.getParameter("m");//动作类型参数
		if("list".equals(m)){
			process(request, response, "/page/institution/institution_list.jsp");
		}else if("input".equals(m)){
			process(request, response, "/page/institution/institution_add.jsp");
		}else if("get".equals(m)){
			process(request, response, "/page/institution/institution_edit.jsp");
		}else if("add".equals(m)){
			add(request, response);
			process(request, response, "/page/institution/institution_list.jsp");
		}else if("edit".equals(m)){
			edit(request, response);
			process(request, response, "/page/institution/institution_list.jsp");
		}else if("del".equals(m)){
			del(request, response);
			process(request, response, "/page/institution/institution_list.jsp");
		}else {
			error(request, response);
		}
	}
	/**
	 * 添加机构方法
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException{
		 
		 try{
			//接收页面传递过来的参数
			 String areacode=request.getParameter("areacode");
			 String agencode=request.getParameter("agencode");
			 String agenname=request.getParameter("agenname");
			 //实例化Institution类
			 Institution institution =new Institution();
			 int grade=areacode.length()==6?1:2;
			 //调用添加方法
			 institution.addInst(areacode, agencode, agenname, grade);
		 }catch(Exception e){
			 error(request, response);
		 }
		 
	}
	
	private void edit(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			// 接收所属地级+行政区域编码+行政区域名称
			String areacode = req.getParameter("areacode");
			String agencode = req.getParameter("agencode");
			String agenname = req.getParameter("agenname");
			System.out.println("==让我看看地区id=="+areacode+"==让我看看机构id=="+agencode+"==让我看看=="+agenname);
			Institution institution = new Institution(agencode);
			institution.editInstitution(areacode,agenname);
		} catch (Exception e) {
			e.printStackTrace();
			
		}
	}
	
	private void del(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			// 接收行政区域编码+行政区域名称，行政区域名称有可能出现中文乱码
			String agencode = req.getParameter("id");
			 Institution institution =new Institution(agencode);
			 institution.delInstitution();
		} catch (Exception e) {
			e.printStackTrace();
			
		}
	}
}
